package com.housekeeping.housekeeping.administrator.adminsinformation.dao.impl;

import com.housekeeping.housekeeping.administrator.adminsinformation.dao.InsertDao;
import com.housekeeping.housekeeping.administrator.entity.AdminsInformation;
import com.housekeeping.housekeeping.administrator.utils.Dbmanage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InsertDaoImpl implements InsertDao {
    @Override
    public int insert(AdminsInformation admins) {
        int classify_id=0;
        if ("管理".equals(admins.getJurisdiction())){
            classify_id=4;
        }if("维护".equals(admins.getJurisdiction())) {
            classify_id=3;
        }if ("设计".equals(admins.getJurisdiction())){
            classify_id=2;
        }if ("技术".equals(admins.getJurisdiction())){
            classify_id=1;
        }
        Connection connection=null;
        PreparedStatement statement=null;
        int count=0;
        int i = judge(admins.getAccount());
        if (i>0){
            return count;
        }else {
            try {
                connection = Dbmanage.getConnection();
                String sql = "INSERT INTO admins_information(name,account,password,phone_number,jurisdiction,remark,classify_id) VALUES(?,?,?,?,?,?,?)";
                statement = connection.prepareStatement(sql);
                statement.setString(1, "" + admins.getName());
                statement.setString(2, "" + admins.getAccount());
                statement.setString(3, "" + admins.getPassword());
                statement.setString(4, "" + admins.getPhoneNumber());
                statement.setString(5, "" + admins.getJurisdiction());
                statement.setString(6, "" + admins.getRemark());
                statement.setInt(7, classify_id);
                count = statement.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                Dbmanage.close(connection, statement);
            }
            return count;
        }
    }
    public int judge(String account){
        Connection connection=null;
        PreparedStatement statement=null;
        int nums=0;
        try {
            connection=Dbmanage.getConnection();
            String sql="select *from admins_information where account="+"'"+account+"'";
            statement=connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                nums=1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            Dbmanage.close(connection,statement);
        }
        return nums;
    }
}
